package com.findme.fecedetection.ui.activity

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.findme.fecedetection.Const
import com.findme.fecedetection.R
import com.findme.fecedetection.base.ImageViewTang
import com.findme.fecedetection.base.ImmersionTitleActivity
import com.findme.fecedetection.net.HttpManager
import com.findme.fecedetection.utlis.getStringType
import com.tanguy.user.utlis.extend.*
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/11/25 0025 16:31
 */
class PatDetailsActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_pat_details
    override fun initView() {
        super.initView()
        val id = intent.getIntExtra("id", 0)
        loge(id)
        click(R.id.iv_back){finish()}
        click(R.id.tv_focus){showToast("关注")}
        HttpManager.singleDetails(id)
                .requestSucceed(this) {
                    loge(it.toString())
                    find<ImageViewTang>(R.id.iv_picture).setImageURI(it.imageUrl)
                    setText(R.id.tv_location,"发现地点:"+it.location)
                    setText(R.id.tv_colour,"颜色:"+getStringType(Const.Type.CWYS,it.color))
                    setText(R.id.tv_site,it.description)

                    val list = it.imageUrl.replace("{", "").replace("'", "").replace("\"", "").replace("[", "").replace("]", "").replace("}", "").split(",")
                    if (list.size <= 1) return@requestSucceed
                    val arrayList = list as ArrayList<String>
                    arrayList.removeAt(0)
                    find<View>(R.id.view_images).visibility = View.VISIBLE
                    val linearLayout = find<LinearLayout>(R.id.layout_images)
                    linearLayout.removeAllViews()
                    for (i in 0 until arrayList.size) {
                        val imageViewTang = getImageViewTang(linearLayout.context, arrayList[i])
                        linearLayout.addView(imageViewTang)
                    }

                }
    }
    private fun getImageViewTang(context: Context, url: String): ImageViewTang {
        val imageViewTang = ImageViewTang(context)
        imageViewTang.apply {
            setImageURI(url)
            setPadding(context.dp2px(7), 0, 0, 0)
            layoutParams = ViewGroup.LayoutParams(context.dp2px(76), context.dp2px(67))
        }
        return imageViewTang
    }
}